package lesson.user;

import com.alibaba.fastjson.JSON;
import lesson.base.BaseServlet;
import lesson.permission.Role;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet("/user/*")
@Role(User.ROLE_ADMIN)
public class UserServlet extends BaseServlet {

    private UserService userService = new UserService();

    /**
     * 注册用户 /user/register
     * @throws ServletException
     * @throws IOException
     */
    public void register(String role, String name, String pwd) throws ServletException, IOException {
        System.out.println("role:" + role + "name:" + name + "pwd:" + pwd);

        User user = new User();
        user.setName(name);
        user.setPwd(pwd);
        user.setRole(Integer.parseInt(role));

        System.out.println(JSON.toJSONString(user));

        String msg = userService.addUser(user);

        if (msg == null) {
            writeJSON(true);
        } else {
            writeJSON(false, msg);
        }



    }
}
